<?PHP
include("../inc/config.php");
?>
<html>
<head>
	<?PHP
	if($_GET['action'] == 'SAVE') {
		$sep = $_CONF->get('CONFIG>ADMIN>MULTIFIELDSEPARATOR.nodeValue');
		$_CONF->remove('CONFIG>USERS>ACCOUNT.name='.$_GET['userName'].'.node');
		$_CONF->appendNewNode('CONFIG>USERS.node', $_CONF->createNode(array(
			"ACCOUNT",
			"name=".$_POST['username'],
			"pwd=".$_POST['pwd'],
			"type=".$_POST['userType']
		)));
		$_CONF->appendNewNode('CONFIG>USERS>ACCOUNT.name='.$_POST['username'].'.node', $_CONF->createNode(array("TABLES")));
		$tables = explode($sep, $_POST['userTables']);
		foreach($tables as $table) {
			if($table != "") {
				$newNode = $_CONF->createNode(array(
					"table",
					"name=$table"
				));
				$_CONF->appendNewNode('CONFIG>USERS>ACCOUNT.name='.$_POST['username'].'>TABLES.node', $newNode);
			}
		}
		_log("Configurator Users: Modificato/Aggiunto ".$_GET['userName']." => tipo: ".$_POST['userType']);
		$_CONF->saveBuiltXML();
	}
	?>
	<title>Config Users</title>
	<style type="text/css">@import '../css/common.css';</style>
	<script type="text/javascript" src="../js/common.js"></script>
</head>
<body>
	
	<div>
	
	<?PHP
	$sep = $_CONF->get('CONFIG>ADMIN>MULTIFIELDSEPARATOR.nodeValue');
	$myUser = $_CONF->get('CONFIG>USERS>ACCOUNT.name='.((isset($_POST['username']))?$_POST['username']:$_GET['userName']).'.node');
	$userName = $_CONF->get('ACCOUNT.name', $myUser);
	$password = $_CONF->get('ACCOUNT.pwd', $myUser);
	$type = $_CONF->get('ACCOUNT.type', $myUser);
	$tables = $_CONF->get('ACCOUNT>TABLES.childNodes', $myUser);
	$myTables = array();
	foreach($tables as $myTable) {
		array_push($myTables, $myTable['attributes']['NAME']);
	}
	?>
	
	<table cellpadding="5">
	<form action="?action=SAVE&userName=<?=$_GET['userName']?>" method="post">
	<tr><td colspan="3"><h3>Modifica utente</h3></td></tr>
	<tr valign="top"><td><strong>Nome utente:</strong></td><td><input type="text" name="username" value="<?=$userName?>"></td><td rowspan="3"><strong>Tabelle</strong><br>
	<input type="hidden" name="userTables" id="editUserTablesId" value="<?=implode($sep, $myTables);?>">
	<select name="userTables_selection" size="10" multiple="multiple" onChange="FF.configurator.memSelectedTables(this, 'editUserTablesId', '<?=$sep?>');">
	<?
	$tables = $_DB->getTablesName();
	foreach($tables as $table) {
		$title = $_CONF->get("CONFIG>TABLES>TABLE.name=$table.title");
		echo '<option value="'.$table.'" '.((in_array($table, $myTables))?'selected':'').'>'.(($title)?$title:$table).'</option>';
	}
	?>
	</select>
	</td></tr>
	<tr><td><strong>Password:</strong></td><td><input type="password" name="pwd" value="<?=$password?>"></td></tr>
	<tr><td><strong>Tipo utente:</strong></td><td><select name="userType"><option value="0" <?=(($type==0)?"selected":"")?>>Utente semplice</option><option value="1" <?=(($type==1)?"selected":"")?>>Utente amministratore</option><option value="2" <?=(($type==2)?"selected":"")?>>God mode</option></select></td></tr>
	</table>
	
	<div class="popupButtonRow">
		<? if($_GET['action'] == 'SAVE') { ?>
			<strong>Salvato</strong>
		<? } ?>
		<input type="submit" name="invia" value="IMPOSTA">
	</div>
	
</body>
</html>
